package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_goods_activity;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiType;

import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiT_goods_activityService;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.service.ApiTypeService;

import cn.com.util.ImageUtils;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_goods_activityController {

	private static Logger logger = Logger
			.getLogger(T_goods_activityController.class);

	@Autowired
	private ApiT_goods_activityService apiT_goods_activityService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_productsService productsService;
	@Autowired
	private ApiGoodsService apiGoodsService;
	
	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity-list")
	public ModelAndView t_goods_activity_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_list.jsp");

		String cityid=request.getParameter("cityid");
		String act=request.getParameter("act");
		String actid="";
		if(act!=null){
			actid=startOnLoadService.dictionaryMap().get(act);
		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = apiTypeService.findTypes();
		mv.addObject("types", types);

		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService
				.groupByAll(cityid==null?"":cityid,actid==null?"":actid);
		mv.addObject("t_goods_activitys", t_goods_activitys);
		mv.addObject("act",act);

		return mv;
	}
	
	/**
	 * 进入一元购查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_yiyuan-list")
	public ModelAndView t_goods_activity_yiyuan_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_yiyuan_list.jsp");

		String cityid=request.getParameter("cityid");
		String act="ACT_XINREN";
		String actid=null;
		if(act!=null){
			actid=startOnLoadService.dictionaryMap().get(act);
		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = apiTypeService.findTypes();
		mv.addObject("types", types);

		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService
				.groupByAll(cityid==null?"":cityid,actid==null?"":actid);
		mv.addObject("t_goods_activitys", t_goods_activitys);
		mv.addObject("act",act);

		return mv;
	}
	
	

	/**
	 * 进入一元购新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_yiyuan-add-pre")
	public ModelAndView t_goods_activity_yiyuan_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_yiyuan_add.jsp");
		String typeid=request.getParameter("typeid");
		String cityid=request.getParameter("cityid");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");

		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> promotion_types = apiTypeService.findTypesByCmod("营销活动");
		mv.addObject("promotion_types", promotion_types);
		
		String act="ACT_XINREN";
		String actid=null;
		if(act!=null){
			actid=startOnLoadService.dictionaryMap().get(act);
		}
		mv.addObject("actid",actid);
		//
		if (typeid!=null&&cityid!=null&&stime!=null&&etime!=null) {
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,String> m=new HashMap<String,String>();
			m.put("actid", typeid);
			m.put("cityid", cityid);
			m.put("stime", stime);
			m.put("etime", etime);
			list=apiT_goods_activityService.searActTime(m);
			mv.addObject("t_goods_activity", list);
			mv.addObject("productSize",list.size());
		}else{
			mv.addObject("productSize","1");
		}
		return mv;
	}
	
	/**
	 * 一元购新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_yiyuan-add")
	public void t_goods_activity_yiyuan_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		String city_id = request.getParameter("city_id");
		String cid = request.getParameter("cid");
		if(city_id==null)
			city_id= cid;
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String is_finished = request.getParameter("is_finished");
		String ext_info = request.getParameter("ext_info");
		String act_img = request.getParameter("act_img");
		String products_list=request.getParameter("products_list");

		String act="ACT_XINREN";
		String actid=null;
		if(act!=null){
			actid=startOnLoadService.dictionaryMap().get(act);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(products_list!=null&&!"".equals(products_list)){
			String [] one=products_list.split(";");
			for(String s:one){
				String[] two=s.split(",");
				String num=productsService.searStoreByProId(two[3]);
				if(Integer.parseInt(two[4])>0){//更新
					ApiT_goods_activity t_goods_activity = apiT_goods_activityService
							.findT_goods_activityById(Integer.valueOf(two[4]));
	
					t_goods_activity.setCity_id(Integer.valueOf(city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[5]));
					Long _product_id = Long.valueOf(two[3]);
					ApiT_products _product = productsService.findT_productsById(_product_id);
					ApiGoods _goods = apiGoodsService.findT_goodsById(_product.getGoods_id());
					t_goods_activity.setProduct_id(_product_id);
					t_goods_activity.setAct_img(_goods.getImage());
					
					t_goods_activity.setGoods_name(two[8]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					//t_goods_activity.setAct_img(two[6]);
					t_goods_activity.setOldprice(_product.getPrice().toString());
					t_goods_activity.setNewprice("1");	//一元购
					t_goods_activity.setCount(_product.getStore().toString());	//原有库存
					t_goods_activity.setOldcount(_product.getStore().toString());
					//t_goods_activity.setSort();
					
					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.updatePriceAndSNum(Integer.parseInt(two[4]));
						apiT_goods_activityService.updateT_goods_activity(t_goods_activity);	
						productsService.updatePriceByPro("1", _product_id.toString(), _product.getStore().toString());
					}else{
						apiT_goods_activityService.recoveryPriceAndSNum(Integer.parseInt(two[4]));
					}
				}else{//添加
					ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();

					t_goods_activity.setAct_type_id(Integer.valueOf(actid));
					t_goods_activity.setCity_id(Integer.valueOf(city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[5]));
					
					Long _product_id = Long.valueOf(two[3]);
					ApiT_products _product = productsService.findT_productsById(_product_id);
					ApiGoods _goods = apiGoodsService.findT_goodsById(_product.getGoods_id());
					t_goods_activity.setProduct_id(_product_id);
					t_goods_activity.setAct_img(_goods.getImage());
				
					t_goods_activity.setGoods_name(two[8]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					//t_goods_activity.setAct_img(two[6]);
					t_goods_activity.setOldprice(_product.getPrice().toString());
					t_goods_activity.setNewprice("1");	//一元购
					t_goods_activity.setCount(_product.getStore().toString());	//原有库存
					t_goods_activity.setOldcount(_product.getStore().toString());
					//t_goods_activity.setSort();	
					
					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.insertT_goods_activity(t_goods_activity);
						productsService.updatePriceByPro("1", _product_id.toString(), _product.getStore().toString());
					}
				}
			}
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@RequestMapping(value = "t_goods_activity_yiyuan-disable")
	public void t_goods_activity_yiyuan_disable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		int ret_i = apiT_goods_activityService.deleteT_goods_activity(Integer.parseInt(id));
		if(ret_i>0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}else{
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "删除失败！");
		}
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	
	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "act-list")
	public ModelAndView act_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_list.jsp");
		String act=request.getParameter("act");
		String a=startOnLoadService.dictionaryMap().get(act);
		String city=request.getParameter("city");
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = apiTypeService.findTypes();
		mv.addObject("types", types);

		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService
				.searGruopAll(startOnLoadService.dictionaryMap().get(act));
		mv.addObject("t_goods_activitys", t_goods_activitys);

		return mv;
	}
	
	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "act_list_points")
	public ModelAndView act_list_points(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_points_list.jsp");
		String act=request.getParameter("act");
		String a=startOnLoadService.dictionaryMap().get(act);
		String city_id=request.getParameter("city_id");
		String goods_name=request.getParameter("goods_name");
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("act_type_id", a);
		conditions.put("city_id", city_id);
		conditions.put("goods_name", goods_name);
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = apiTypeService.findTypes();
		mv.addObject("types", types);
		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService.searchActivityGoods(conditions, pageInfo);
		mv.addObject("t_goods_activitys", t_goods_activitys);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);

		return mv;
	}
	
	@RequestMapping(value = "act-disable")
	public void act_disable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (apiT_goods_activityService.recoveryPriceAndSNum(Integer.parseInt(id))) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	@RequestMapping(value = "act-disable_2")
	public void act_disable_2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (apiT_goods_activityService.deleteT_goods_activity(Integer.parseInt(id))==1) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity-add-pre")
	public ModelAndView t_goods_activity_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_add.jsp");
		String typeid=request.getParameter("typeid");
		String cityid=request.getParameter("cityid");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");

		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> promotion_types = apiTypeService.findTypesByCmod("营销活动");
		mv.addObject("promotion_types", promotion_types);

		//
		if (typeid!=null&&cityid!=null&&stime!=null&&etime!=null) {
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,String> m=new HashMap<String,String>();
			m.put("actid", typeid);
			m.put("cityid", cityid);
			m.put("stime", stime);
			m.put("etime", etime);
			list=apiT_goods_activityService.searActTime(m);
			mv.addObject("t_goods_activity", list);
			mv.addObject("productSize",list.size());
		}else{
			mv.addObject("productSize","1");
		}
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_points_add_pre")
	public ModelAndView t_goods_activity_points_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_points_add.jsp");
		String typeid=request.getParameter("typeid");
		String cityid=request.getParameter("cityid");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");
		
		String actType = request.getParameter("actType");
		String a=startOnLoadService.dictionaryMap().get(actType);
		
		//--类型只能选择积分商城
		System.out.println("类型-"+typeid+",cityid"+cityid+",stime-"+stime+",etime-"+etime);

		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> promotion_types = new ArrayList<ApiType>();
		ApiType apiType = apiTypeService.findTypeById(Integer.valueOf(a));
		promotion_types.add(apiType);
		mv.addObject("promotion_types", promotion_types);

		//
		if (typeid!=null&&cityid!=null&&stime!=null&&etime!=null) {
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,String> m=new HashMap<String,String>();
			m.put("actid", typeid);
			m.put("cityid", cityid);
			m.put("stime", stime);
			m.put("etime", etime);
			list=apiT_goods_activityService.searActTime(m);
			mv.addObject("t_goods_activity", list);
			mv.addObject("productSize",list.size());
		}else{
			mv.addObject("productSize","1");
		}
		return mv;
	}
	
	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_points_add")
	public void t_goods_activity_points_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String act_name = request.getParameter("act_name");
		String act_desc = request.getParameter("act_desc");
		String act_type_id = request.getParameter("act_type_id");
		String city_id = request.getParameter("city_id");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String products_list=request.getParameter("products_list");
		System.out.println("products_list-"+products_list);

		Map<String, Object> map = new HashMap<String, Object>();
		
		if(products_list!=null){
			String [] one=products_list.split(";");
			for(String s:one){
				String[]two=s.split(",");
				if(Integer.parseInt(two[2])>0){//更新
					ApiT_goods_activity t_goods_activity = apiT_goods_activityService
							.findT_goods_activityById(Integer.valueOf(two[2]));
					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setAct_type_id(Integer.valueOf(act_type_id));
					t_goods_activity.setCity_id(Integer.valueOf(city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[3]));
					t_goods_activity.setProduct_id(Long.valueOf(two[1]));
					t_goods_activity.setGoods_name(two[5]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(0);
					t_goods_activity.setAct_img(two[4]);
					t_goods_activity.setOldprice(two[0]);
					t_goods_activity.setPoints(Integer.valueOf(two[6]));
					
					apiT_goods_activityService.updateT_goods_activity(t_goods_activity);
					
				}else{//添加
					ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();

					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setAct_type_id(Integer.valueOf(act_type_id));
					t_goods_activity.setCity_id(Integer.valueOf(city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[3]));
					t_goods_activity.setProduct_id(Long.valueOf(two[1]));
					t_goods_activity.setGoods_name(two[5]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(0);
					t_goods_activity.setAct_img(two[4]);
					t_goods_activity.setOldprice(two[0]);
					t_goods_activity.setPoints(Integer.valueOf(two[6]));

					apiT_goods_activityService.insertT_goods_activity(t_goods_activity);
				}
			}
		}
		
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity-add")
	public void t_goods_activity_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String act_id = request.getParameter("act_id");
		String act_name = request.getParameter("act_name");
		String act_desc = request.getParameter("act_desc");
		String act_type_id = request.getParameter("act_type_id");
		String city_id = request.getParameter("city_id");
		String goods_id = request.getParameter("goods_id");
		String product_id = request.getParameter("product_id");
		String goods_name = request.getParameter("goods_name");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String is_finished = request.getParameter("is_finished");
		String ext_info = request.getParameter("ext_info");
		String act_img = request.getParameter("act_img");
		String products_list=request.getParameter("products_list");
		String actid=request.getParameter("atid");
		String cid=request.getParameter("cid");
		String roll=request.getParameter("roll");

		Map<String, Object> map = new HashMap<String, Object>();
		
		if(products_list!=null&&!"".equals(products_list)){
			String [] one=products_list.split(";");
			for(String s:one){
				String[]two=s.split(",");
				String num=productsService.searStoreByProId(two[3]);
				if(Integer.parseInt(two[4])>0){//更新
					ApiT_goods_activity t_goods_activity = apiT_goods_activityService
							.findT_goods_activityById(Integer.valueOf(two[4]));
					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setGoods_id(Long.valueOf(two[5]));
					t_goods_activity.setProduct_id(Long.valueOf(two[3]));
					t_goods_activity.setGoods_name(two[8]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					t_goods_activity.setAct_img(two[6]);
					t_goods_activity.setOldprice(two[1]);
					t_goods_activity.setNewprice(two[0]);
					t_goods_activity.setCount(two[2]);
					t_goods_activity.setOldcount(num);
					t_goods_activity.setSort(two[7]);
					t_goods_activity.setRoll(roll);
					
					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.updatePriceAndSNum(Integer.parseInt(two[4]));
						apiT_goods_activityService.updateT_goods_activity(t_goods_activity);
						productsService.updatePriceByPro(two[0], two[3],two[2]);
					}else{
						apiT_goods_activityService.recoveryPriceAndSNum(Integer.parseInt(two[4]));
					}
					
				}else{//添加
					ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();

					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setAct_type_id(Integer.valueOf(act_type_id==null?actid:act_type_id));
					t_goods_activity.setCity_id(Integer.valueOf(city_id==null?cid:city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[5]));
					t_goods_activity.setProduct_id(Long.valueOf(two[3]));
					t_goods_activity.setGoods_name(two[8]);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					t_goods_activity.setAct_img(two[6]);
					t_goods_activity.setOldprice(two[1]);
					t_goods_activity.setNewprice(two[0]);
					t_goods_activity.setCount(two[2]);
					t_goods_activity.setOldcount(num);
					t_goods_activity.setSort(two[7]);
					t_goods_activity.setRoll(roll);

					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.insertT_goods_activity(t_goods_activity);
						productsService.updatePriceByPro(two[0], two[3],two[2]);
					}
				}
			}
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 上传缩略图
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "t_goods_activity-img-upload")
	public void t_goods_activity_img_upload(
			@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// ModelMap model

		String path = ImageUtils.getServletWebAppsPath(request)+"/api/ads/";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String sdfStr = sdf.format(new Date());
		String fileName = sdfStr + "_" + file.getOriginalFilename();

		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		String smallpic_path = "";
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			file.transferTo(targetFile);
			smallpic_path = ImageUtils.getDataBaseImagePath()+"/api/ads/" + fileName;

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", smallpic_path);

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity-delete")
	public void t_goods_activity_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_goods_activityService.deleteT_goods_activity(Integer
				.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 上传头像
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "uploadImg")
	public @ResponseBody String uploadImg(@RequestParam(value = "file") MultipartFile file,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String fileName="";
		try {
			String realpath = ImageUtils.getServletWebAppsPath(request)+ "/api/ads/";// 获取服务器路径
			realpath = realpath.replace("\\", "/");
			// 生产的图片名称：
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String sdfStr = sdf.format(new Date());
			String[] fi=file.getOriginalFilename().split("\\.");
			
			fileName = sdfStr + "." + fi[fi.length-1];
			File targetFile = new File(realpath, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			file.transferTo(new File(realpath,fileName));

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功");
				
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		return ImageUtils.getDataBaseImagePath()+"/api/ads/"+fileName;
	}
	
	/**
	 * 检查活动时间
	 * 
	 * @return
	 */
	@RequestMapping(value = "checkActTime")
	public @ResponseBody Integer checkActTime(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String actid=request.getParameter("actid");
		String cityid=request.getParameter("cityid");
		String time=request.getParameter("time");
		String time1=request.getParameter("time1");
		String oldetime=request.getParameter("oldetime");
		String oldstime=request.getParameter("oldstime");
		String charge_money = request.getParameter("charge_money");
		if(actid!=null&&!"".equals(actid)&&cityid!=null&&!"".equals(cityid)&&time!=null&&!"".equals(time)&&time1!=null&&!"".equals(time1)){
			if(apiT_goods_activityService.checkActTime(actid, cityid, time, oldetime, oldstime, time1,charge_money)==0){
				return 0;//可以添加
			}
		}
		return 1;//不可以添加
	}
	
	/**
	 * 进入查询界面--充值有礼
	 * 
	 * @return
	 */
	@RequestMapping(value = "act_list_charge")
	public ModelAndView act_list_charge(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
		"forward:/backstage/t_goods_activity_charge_list.jsp");

		String cityid=request.getParameter("cityid");
		String act=request.getParameter("act");
		String actid="-1";
//		if(act!=null){
//			actid=startOnLoadService.dictionaryMap().get(act);
//		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		
		List<ApiType> types = apiTypeService.findTypes();
		mv.addObject("types", types);
		
		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService
				.groupByAll(cityid==null?"":cityid,actid==null?"":actid);
		mv.addObject("t_goods_activitys", t_goods_activitys);
		mv.addObject("act",act);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面--充值有礼
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_charge-add-pre")
	public ModelAndView t_goods_activity_charge_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_activity_charge_add.jsp");
		String typeid=request.getParameter("typeid");
		String cityid=request.getParameter("cityid");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");

		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> promotion_types = apiTypeService.findTypesByCmod("营销活动");
		mv.addObject("promotion_types", promotion_types);

		//
		if (typeid!=null&&cityid!=null&&stime!=null&&etime!=null) {
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,String> m=new HashMap<String,String>();
			m.put("actid", typeid);
			m.put("cityid", cityid);
			m.put("stime", stime);
			m.put("etime", etime);
			list=apiT_goods_activityService.searActTime(m);
			mv.addObject("t_goods_activity", list);
			mv.addObject("productSize",list.size());
		}else{
			mv.addObject("productSize","1");
		}
		return mv;
	}
	
	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_activity_charge-add")
	public void t_goods_activity_charge_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String act_name = request.getParameter("act_name");
		String act_desc = request.getParameter("act_desc");
		String act_type_id = request.getParameter("act_type_id");
		String city_id = request.getParameter("city_id");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String is_finished = request.getParameter("is_finished");
		String products_list=request.getParameter("products_list");
		String actid=request.getParameter("atid");
		String cid=request.getParameter("cid");
		String charge_money = request.getParameter("charge_money");

		Map<String, Object> map = new HashMap<String, Object>();
		
		if(products_list!=null&&!"".equals(products_list)){
			String [] one=products_list.split(";");
			for(String s:one){
				String[]two=s.split(",");
				if(Integer.parseInt(two[1])>0){//更新
					ApiT_goods_activity t_goods_activity = apiT_goods_activityService
							.findT_goods_activityById(Integer.valueOf(two[1]));
					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setCharge_money(charge_money);
					t_goods_activity.setGoods_id(Long.valueOf(two[2]));
					t_goods_activity.setProduct_id(Long.valueOf(two[0]));
					t_goods_activity.setGoods_name(two[4]);
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					t_goods_activity.setAct_img(two[3]);
					
					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.updateT_goods_activity(t_goods_activity);
					}
					
				}else{//添加
					ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();

					t_goods_activity.setAct_name(act_name);
					t_goods_activity.setAct_desc(act_desc);
					t_goods_activity.setCharge_money(charge_money);
					t_goods_activity.setAct_type_id(Integer.valueOf(act_type_id==null?actid:act_type_id));
					t_goods_activity.setCity_id(Integer.valueOf(city_id==null?cid:city_id));
					t_goods_activity.setGoods_id(Long.valueOf(two[2]));
					t_goods_activity.setProduct_id(Long.valueOf(two[0]));
					t_goods_activity.setGoods_name(two[4]);
					t_goods_activity.setStart_time(start_time);
					t_goods_activity.setEnd_time(end_time);
					t_goods_activity.setIs_finished(Integer.valueOf(is_finished));
					t_goods_activity.setAct_img(two[3]);

					if(Integer.valueOf(is_finished)==0){
						apiT_goods_activityService.insertT_goods_activity(t_goods_activity);
					}
				}
			}
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
